<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Think\Model\RelationModel;

class AdsModel extends RelationModel {

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function deleteById($id){
        // 删除详情
        $res1 = M('ads_detail')->where(['ads_id'=>$id])->save(['status'=>0]);
        // 删除
        $res2 = $this->where(['id'=>$id])->save(['status'=>0]);
        if($res1!==false && $res2!==false){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取广告位详情
     * @param $id
     * @return mixed
     */
    public function getInfoById($id){
        $data = $this->where(['id'=>$id, 'status'=>'1'])->find();
        $data['rectangleArr'] = M('ads_detail')->where(['type'=>'1', 'ads_id'=>$id, 'status'=>'1'])->order('id asc')->select();
        $data['circleArr'] = M('ads_detail')->where(['type'=>'2', 'ads_id'=>$id, 'status'=>'1'])->order('id asc')->select();
        return $data;
    }

    /**
     * 生成预览图
     * @param $params
     * @return array|string
     */
    public function makeImg($params){
        if($params['ad_name'] == ''){
            return ['status' => false, 'msg' => '广告名不能为空'];
        }

        if(!$params['width'] || !$params['height']){
            return ['status' => false, 'msg' => '广告尺寸不能为空'];
        }


        // 矩形格式： rectangleArr = [{index:0,x_axis:0,y_axis:0,width:0,height:0,ads_name:'',key:'',metal:'',remark:''}]
        // 圆形格式： circleArr = [{index:0,x_axis:0,y_axis:0,radius:0,ads_name:'',key:'',metal:'',remark:''}]
        if(empty($params['rectangleArr']) && empty($params['circleArr'])){
            return ['status' => false, 'msg' => '请输入广告位具体参数'];
        }


        $width = $params['width'];                                              //图像宽度
        $height = $params['height'];                                             //图像高度
        $img = imagecreatetruecolor($width,$height);               //创建图像
        $white = imagecolorallocate($img,255,255,255);             //白色
        $black = imagecolorallocate($img,0,0,0);                   //黑色
        $grey = imagecolorallocate($img,220,220,220);              //灰色
        $blue = imagecolorallocate($img,238,44,44);              //填充色
        imagefill($img,0,0,$grey);                                  //将背景设置为灰色

        //矩形
        foreach ($params['rectangleArr'] as $k => $v) {
            if($k%3 == 0){
                $blue = imagecolorallocate($img,255,153,102);
            }elseif($k%3 == 1){
                $blue = imagecolorallocate($img,255,102,102);
            }elseif($k%3 == 2){
                $blue = imagecolorallocate($img,255,204,204);
            }
            $font = ROOT_PATH . '/Public/font/tt.OTF';

            imagefilledrectangle($img,$v['x_axis'],$v['y_axis'],$v['x_axis']+$v['width'],$v['y_axis']+$v['height'],$blue);
            imagettftext($img, 12, 0, $v['x_axis']+20, $v['y_axis']+20, $black, $font, $v['ads_name']);
            imagettftext($img, 12, 0, $v['x_axis']+20, $v['y_axis']+60, $black, $font, $v['width'].'*'.$v['height']);
        }

        //圆形
        foreach ($params['circleArr'] as $k => $v) {

            if($k%3 == 0){
                $blue = imagecolorallocate($img,255,153,102);
            }elseif($k%3 == 1){
                $blue = imagecolorallocate($img,255,102,102);
            }elseif($k%3 == 2){
                $blue = imagecolorallocate($img,255,204,204);
            }
            $font = ROOT_PATH . 'Public/font/tt.OTF';

            imagefilledellipse($img,$v['x_axis'] + ($v['radius']/2),$v['y_axis']+ ($v['radius']/2),$v['radius'],$v['radius'],$blue);
            imagettftext($img, 12, 0, $v['x_axis']+ ($v['radius']/2), $v['y_axis']+ ($v['radius']/2) + 20, $black, $font, $v['ads_name']);
            imagettftext($img, 12, 0, $v['x_axis']+ ($v['radius']/2), $v['y_axis']+ ($v['radius']/2)+ 40, $black, $font, $v['radius']);
        }


        header("content-type: image/png");                    //输出图像的MIME类型

        $path = "Uploads/gd/".date("Y-m-d");
        if(!is_dir($path)){
            mkdir($path,2770,true);
        }

        $file = $path."/".rand(11111111,99999999).".png";

        imagepng($img,$file);
        imagedestroy($img);                                      //清空内存
        return $file;
    }

    /**
     * 添加和修改
     * @param $param
     * @return bool
     */
    public function edit($param){
        $now = time();
        if($param['type'] == 'add'){
            // 添加
            $param['add_time'] = time();
            $param['update_time'] = time();

            $editParam = $param;
            unset($editParam['type']);
            unset($editParam['rectangleArr']);
            unset($editParam['circleArr']);
            $res1 = $this->add($editParam);
            // 矩形
            foreach($param['rectangleArr'] as &$v){
                // 添加
                $v['add_time'] = $now;
                $v['update_time'] = $now;
                $v['ads_id'] = $res1;
                unset($v['index']);
                $res = M('ads_detail')->add($v);
                if($res===false){
                    return false;
                }
            }
            unset($v);

            // 圆形
            foreach($param['circleArr'] as &$v){
                // 添加
                $v['add_time'] = $now;
                $v['update_time'] = $now;
                $v['ads_id'] = $res1;
                unset($v['index']);
                $res = M('ads_detail')->add($v);
                if($res===false){
                    return false;
                }
            }
            unset($v);

            if($res1){
                return true;
            }
        }elseif($param['type'] == 'edit'){
            // 修改
            $param['update_time'] = time();
            $editParam = $param;
            unset($editParam['type']);
            unset($editParam['rectangleArr']);
            unset($editParam['circleArr']);
            $res1 = $this->where(['id'=>$param['id']])->save($editParam);
            // 矩形
            $res2 = M('ads_detail')->where(['ads_id'=>$param['id'], 'type'=>'1'])->save(['status'=>0]);
            foreach($param['rectangleArr'] as &$v){
                if($v['id']){
                    // 修改
                    $v['status'] = '1';
                    $v['update_time'] = time();
                    unset($v['index']);
                    $res = M('ads_detail')->where(['id'=>$v['id']])->save($v);
                    if($res===false){
                        return false;
                    }
                }else{
                    // 添加
                    $v['add_time'] = $now;
                    $v['update_time'] = $now;
                    $v['ads_id'] = $editParam['id'];
                    unset($v['index']);
                    $res = M('ads_detail')->add($v);
                    if($res===false){
                        return false;
                    }
                }
            }
            unset($v);

            // 圆形
            $res3 = M('ads_detail')->where(['ads_id'=>$param['id'], 'type'=>'2'])->save(['status'=>0]);
            foreach($param['circleArr'] as &$v){
                if($v['id']){
                    // 修改
                    $v['status'] = '1';
                    $v['update_time'] = time();
                    $res = M('ads_detail')->where(['id'=>$v['id']])->save($v);
                    if($res===false){
                        return false;
                    }
                }else{
                    // 添加
                    $v['add_time'] = $now;
                    $v['update_time'] = $now;
                    $v['ads_id'] = $editParam['id'];
                    $res = M('ads_detail')->add($v);
                    if($res===false){
                        return false;
                    }
                }
            }
            unset($v);

            if($res1!==false && $res2!==false && $res3!==false){
                return true;
            }
        }
    }
}
